import { MessageManager } from "../../common/event/MessageManager";
import { UIManager } from "../../common/ui/UIManager";
import { GameData } from "./GameData";



const { ccclass, property } = cc._decorator;

@ccclass('GameManager')
export class GameManager {
    private static _instance: GameManager = null;
    private _ui: UIManager = null!;
    private _gameData: GameData = null;             // 游戏数据
    private _message: MessageManager = MessageManager.Instance;     // 消息监听
    private _remoteCache: Map<string, cc.SpriteFrame> = new Map();


    get ui() {
        return this._ui;
    }

    set ui(val: UIManager) {
        this._ui = val;
    }

    set gameData(v: GameData) {
        this._gameData = v;
    }

    get gameData() {
        return this._gameData;
    }

    get message() {
        return this._message;
    }

    public static getInstance() {
        if (this._instance == null) {
            this._instance = new GameManager();
            this._instance.init();
        }
        return this._instance;
    }

    init() {
        this.gameData = new GameData();
    }








}


export function debounce(timeout: number = 400) {
    return function (target: any, prop_name: string, descriptor: PropertyDescriptor) {
        const oldValue = descriptor.value;
        let timer = null, count = 0;
        descriptor.value = function () {
            const args = arguments;
            clearTimeout(timer);
            if (count === 0) {// 第一次立即执行
                oldValue.apply(this, args);
                count++;
                timer = setTimeout(() => {
                    count = 0;
                }, timeout);
            } else {
                count++;
                timer = setTimeout(() => {
                    count = 0;
                }, timeout);
            }
        };
        return descriptor;
    };
}

